﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using System.Web.UI;
using System.Web;
using System.Reflection;
using DP.Common;


namespace CRM.PlugIn
{
    public class ComplaintCase
    {
        private T_CONFIG_BLL configBll = new T_CONFIG_BLL();
        private T_SMS_SEND_BLL smsSendBll = new T_SMS_SEND_BLL();
        T_CASEDISPATCHINGHISTORY_BLL caseDispatchingHistoryBll = new T_CASEDISPATCHINGHISTORY_BLL();

        /// <summary>
        /// Updates the dispatching time.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void Dispatching(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            if (entity.DISPATCHINGDELAY.HasValue)
            {
                entity.DISPATCHINGTIME = DateTime.Now.AddHours(entity.DISPATCHINGDELAY.Value);
            }
            else
            {
                entity.DISPATCHINGTIME = DateTime.Now;
            }
            entity.SMSSTATUS = 0;
            Page page = HttpContext.Current.Handler as Page;
            if (page == null) return;

            UserLoginInfo _userLoginInfo = UserLoginInfo.GetLoginInfo();

            if (entity.DISPATCHINGTYPE.ToString() == ((int)DispatchingType.短信派工).ToString())
            {
                LogHelper.WriteLog("Log_DP_PlugIn", "PROCESSINGUSERNAME:" + entity.PROCESSINGUSERNAME);
                T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
                string bodys = string.Empty;
                if (String.IsNullOrEmpty(entity.PROCESSINGUSERNAME.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                string[] tels = entity.PROCESSINGUSERNAME.Trim().Split(new char[] { ';', ',', '；', '，' });
                if (tels.Length == 0)
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                bodys = configBll.GetCacheValueByName(page, "ComplaintSMSDispatchingTemplate");

                bodys = ReplaceStringValue(entity, bodys);

                //LogHelper.WriteLog("Log_DP_PlugIn", bodys.ToString());

                smsSendEntity.BODYS = bodys;
                smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                smsSendEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                smsSendEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                smsSendEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                smsSendEntity.SUBJECTS = "投诉派工:" + entity.CASEID;
                smsSendEntity.CREATETIME = entity.DISPATCHINGTIME;
                smsSendEntity.UINFO = _userLoginInfo.UserEntity.USERNAME.ToString();
                string tempTel = "";
                foreach (var tel in tels)
                {
                    if (tel.Length == 0)
                    {
                        continue;
                    }
                    if (tel.IndexOf('*') >= 0)
                    {
                        tempTel = tel.Substring(0, tel.IndexOf('*'));
                    }
                    if (tempTel.Length == 0)
                    {
                        continue;
                    }
                    smsSendEntity.TEL_TO = tempTel;
                    smsSendBll.Insert(smsSendEntity);

                }
            }

            try
            {
                T_CASEDISPATCHINGHISTORY_Entity caseDispatchingHistoryEntity = new T_CASEDISPATCHINGHISTORY_Entity();
                caseDispatchingHistoryBll._dal.ObjectCopyTo(entity, caseDispatchingHistoryEntity);
                caseDispatchingHistoryEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                caseDispatchingHistoryEntity.CREATETIME = DateTime.Now;
                caseDispatchingHistoryEntity.DISPATCHINGPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CASETYPE = (int)WorkOrderType.投诉工单;
                caseDispatchingHistoryBll.Insert(caseDispatchingHistoryEntity);

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Log_DP_PlugIn", ex.Message);
            }




        }

        /// <summary>
        /// Updates the dispatching time.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void ReDispatching(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            entity.DISPATCHINGTIME = DateTime.Now;
            entity.SMSSTATUS = 0;
            Page page = HttpContext.Current.Handler as Page;
            if (page == null) return;

            UserLoginInfo _userLoginInfo = UserLoginInfo.GetLoginInfo();

            if (entity.DISPATCHINGTYPE.ToString() == ((int)DispatchingType.短信派工).ToString())
            {
                LogHelper.WriteLog("Log_DP_PlugIn", "PROCESSINGUSERNAME:" + entity.PROCESSINGUSERNAME);
                T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
                string bodys = string.Empty;
                if (String.IsNullOrEmpty(entity.PROCESSINGUSERNAME.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                string[] tels = entity.PROCESSINGUSERNAME.Trim().Split(new char[] { ';', ',', '；', '，' });
                if (tels.Length == 0)
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                bodys = configBll.GetCacheValueByName(page, "ComplaintSMSDispatchingTemplate");

                bodys = ReplaceStringValue(entity, bodys);

                //LogHelper.WriteLog("Log_DP_PlugIn", bodys.ToString());

                smsSendEntity.BODYS = bodys;
                smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                smsSendEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                smsSendEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                smsSendEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                smsSendEntity.SUBJECTS = "投诉派工:" + entity.CASEID;
                smsSendEntity.CREATETIME = entity.DISPATCHINGTIME;
                smsSendEntity.UINFO = _userLoginInfo.UserEntity.USERNAME.ToString();
                string tempTel = "";
                foreach (var tel in tels)
                {
                    if (tel.Length == 0)
                    {
                        continue;
                    }
                    if (tel.IndexOf('*') >= 0)
                    {
                        tempTel = tel.Substring(0, tel.IndexOf('*'));
                    }
                    if (tempTel.Length == 0)
                    {
                        continue;
                    }
                    smsSendEntity.TEL_TO = tempTel;
                    smsSendBll.Insert(smsSendEntity);

                }
            }

            try
            {
                T_CASEDISPATCHINGHISTORY_Entity caseDispatchingHistoryEntity = new T_CASEDISPATCHINGHISTORY_Entity();
                caseDispatchingHistoryBll._dal.ObjectCopyTo(entity, caseDispatchingHistoryEntity);
                caseDispatchingHistoryEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                caseDispatchingHistoryEntity.CREATETIME = DateTime.Now;
                caseDispatchingHistoryEntity.DISPATCHINGPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CASETYPE = (int)WorkOrderType.投诉工单;
                caseDispatchingHistoryBll.Insert(caseDispatchingHistoryEntity);

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Log_DP_PlugIn", ex.Message);
            }




        }
        
        /// <summary>
        /// Processings the completed.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void ProcessingCompleted(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            entity.PROCESSINGCOMPLETEDTIME = DateTime.Now;
            if (entity.DISPATCHINGTIME.HasValue && entity.PROCESSINGCOMPLETEDTIME.HasValue)
            {
                entity.COMPLETELENGTH = (entity.PROCESSINGCOMPLETEDTIME.Value - entity.DISPATCHINGTIME.Value).TotalHours;
                //LogHelper.WriteLog("Log_DP_PlugIn", String.Format("DISPATCHINGTIME:{0}--PROCESSINGCOMPLETEDTIME:{1}--COMPLETELENGTH:{2}", entity.DISPATCHINGTIME, entity.PROCESSINGCOMPLETEDTIME, entity.COMPLETELENGTH));
            }



        }

        /// <summary>
        /// Updates the dispatching time.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void VisitDispatching(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            if (!entity.FIRSTVISITTIME.HasValue)
            {
                entity.FIRSTVISITTIME = DateTime.Now;
            }
            entity.COMPLETELENGTH = null;
            entity.DISPATCHINGTIME = DateTime.Now;
            entity.SMSSTATUS = 0;
            Page page = HttpContext.Current.Handler as Page;
            if (page == null) return;

            UserLoginInfo _userLoginInfo = UserLoginInfo.GetLoginInfo();

            if (entity.DISPATCHINGTYPE.ToString() == ((int)DispatchingType.短信派工).ToString())
            {
                LogHelper.WriteLog("Log_DP_PlugIn", "PROCESSINGUSERNAME:" + entity.PROCESSINGUSERNAME);
                T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
                string bodys = string.Empty;
                if (String.IsNullOrEmpty(entity.PROCESSINGUSERNAME.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                string[] tels = entity.PROCESSINGUSERNAME.Trim().Split(new char[] { ';', ',', '；', '，' });
                if (tels.Length == 0)
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                bodys = configBll.GetCacheValueByName(page, "ComplaintSMSDispatchingTemplate");

                bodys = ReplaceStringValue(entity, bodys);

                //LogHelper.WriteLog("Log_DP_PlugIn", bodys.ToString());

                smsSendEntity.BODYS = bodys;
                smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                smsSendEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                smsSendEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                smsSendEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                smsSendEntity.SUBJECTS = "投诉派工:" + entity.CASEID;
                smsSendEntity.CREATETIME = DateTime.Now;
                smsSendEntity.UINFO = _userLoginInfo.UserEntity.USERNAME.ToString();
                string tempTel = "";
                foreach (var tel in tels)
                {
                    if (tel.Length == 0)
                    {
                        continue;
                    }
                    if (tel.IndexOf('*') >= 0)
                    {
                        tempTel = tel.Substring(0, tel.IndexOf('*'));
                    }
                    if (tempTel.Length == 0)
                    {
                        continue;
                    }
                    smsSendEntity.TEL_TO = tempTel;
                    smsSendBll.Insert(smsSendEntity);

                }
            }

            try
            {
                T_CASEDISPATCHINGHISTORY_Entity caseDispatchingHistoryEntity = new T_CASEDISPATCHINGHISTORY_Entity();
                caseDispatchingHistoryBll._dal.ObjectCopyTo(entity, caseDispatchingHistoryEntity);
                caseDispatchingHistoryEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                caseDispatchingHistoryEntity.CREATETIME = DateTime.Now;
                caseDispatchingHistoryEntity.DISPATCHINGPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CASETYPE = (int)WorkOrderType.投诉工单;
                caseDispatchingHistoryBll.Insert(caseDispatchingHistoryEntity);

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Log_DP_PlugIn", ex.Message);
            }




        }

        /// <summary>
        /// Visits the completed.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void VisitCompleted(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            if (!entity.FIRSTVISITTIME.HasValue)
            {
                entity.FIRSTVISITTIME = DateTime.Now;
            }
            entity.VISITTIME = DateTime.Now;

            if (String.IsNullOrEmpty(entity.ISVISITCOMPLATE.Trim()))
            {
                result.Result = MethodResult.ResultType.Failure;
                result.Message = "是否回访成功不能为空！";
                return;
            }
            if (entity.ISVISITCOMPLATE.ToString() == ((int)IsVisitComplate.已回访成功).ToString())
            {
                if (String.IsNullOrEmpty(entity.VISITSITUATION.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "回访情况不能为空！";
                    return;
                }
                if (String.IsNullOrEmpty(entity.SERVICESATISFACTION.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "服务满意度不能为空！";
                    return;
                }
                if (String.IsNullOrEmpty(entity.PRODUCTSATISFACTION.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "产品满意度不能为空！";
                    return;
                }

            }
        }
        
        /// <summary>
        /// Agains the dispatching.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void AgainDispatching(T_COMPLAINTCASE_Entity entity, MethodResult result)
        {
            if (!entity.FIRSTVISITTIME.HasValue)
            {
                entity.FIRSTVISITTIME = DateTime.Now;
            }
            entity.COMPLETELENGTH = null;
            entity.COMPLETEDCONTENT = null;
            entity.COMPLETEDPEOPLE = null;
            entity.COMPLETEDTIME = null;
            entity.PROCESSINGCOMPLETEDTIME = null;

            if (entity.RECEIVETIME != null)
            {
                entity.SMSSTATUS = (int)ComplaintReplyStatus.已回复;
            }
            else
            {
                entity.SMSSTATUS = 0;
            }

            Page page = HttpContext.Current.Handler as Page;
            if (page == null) return;

            UserLoginInfo _userLoginInfo = UserLoginInfo.GetLoginInfo();

            if (entity.DISPATCHINGTYPE.ToString() == ((int)DispatchingType.短信派工).ToString())
            {
                LogHelper.WriteLog("Log_DP_PlugIn", "PROCESSINGUSERNAME:" + entity.PROCESSINGUSERNAME);
                T_SMS_SEND_Entity smsSendEntity = new T_SMS_SEND_Entity();
                string bodys = string.Empty;
                if (String.IsNullOrEmpty(entity.PROCESSINGUSERNAME.Trim()))
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                string[] tels = entity.PROCESSINGUSERNAME.Trim().Split(new char[] { ';', ',', '；', '，' });
                if (tels.Length == 0)
                {
                    result.Result = MethodResult.ResultType.Failure;
                    result.Message = "派工人电话号码不能为空！";
                    return;
                }
                bodys = configBll.GetCacheValueByName(page, "ComplaintSMSDispatchingTemplate");

                bodys = ReplaceStringValue(entity, bodys);

                //LogHelper.WriteLog("Log_DP_PlugIn", bodys.ToString());

                smsSendEntity.BODYS = bodys;
                smsSendEntity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                smsSendEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                smsSendEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                smsSendEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                smsSendEntity.SUBJECTS = "投诉派工:" + entity.CASEID;
                smsSendEntity.CREATETIME = DateTime.Now;
                smsSendEntity.UINFO = _userLoginInfo.UserEntity.USERNAME.ToString();
                string tempTel = "";
                foreach (var tel in tels)
                {
                    if (tel.Length == 0)
                    {
                        continue;
                    }
                    if (tel.IndexOf('*') >= 0)
                    {
                        tempTel = tel.Substring(0, tel.IndexOf('*'));
                    }
                    if (tempTel.Length == 0)
                    {
                        continue;
                    }
                    smsSendEntity.TEL_TO = tempTel;
                    smsSendBll.Insert(smsSendEntity);

                }

            }

            try
            {
                T_CASEDISPATCHINGHISTORY_Entity caseDispatchingHistoryEntity = new T_CASEDISPATCHINGHISTORY_Entity();
                caseDispatchingHistoryBll._dal.ObjectCopyTo(entity, caseDispatchingHistoryEntity);
                caseDispatchingHistoryEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                caseDispatchingHistoryEntity.CREATETIME = DateTime.Now;
                caseDispatchingHistoryEntity.DISPATCHINGPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                caseDispatchingHistoryEntity.CASETYPE = (int)WorkOrderType.投诉工单;
                caseDispatchingHistoryEntity.OPERATIONNAME = "再次派工";
                caseDispatchingHistoryBll.Insert(caseDispatchingHistoryEntity);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Log_DP_PlugIn", ex.Message);
            }


        }

        /// <summary>
        /// Visits the follow.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="result">The result.</param>
        public void VisitFollow(T_REPAIRCASE_Entity entity, MethodResult result)
        {
            if (!entity.FIRSTVISITTIME.HasValue)
            {
                entity.FIRSTVISITTIME = DateTime.Now;
            }


        }

        public string ReplaceStringValue(T_COMPLAINTCASE_Entity entity, string input)
        {
            string temp = input.ToLower();
            PropertyInfo[] propertyInfos = entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetValue(entity, null) != null)
                {
                    temp = temp.Replace("[" + propertyInfo.Name.ToLower() + "]", propertyInfo.GetValue(entity, null).ToString());
                }
                else
                {
                    temp = temp.Replace("[" + propertyInfo.Name.ToLower() + "]", "");
                }
            }
            return temp;
        }
    }
}
